package com.putao.album.message;

import android.os.Bundle;

import com.putao.album.application.GlobalApplication;
import com.putao.album.db.MessageHelper;
import com.putao.album.db.tables.InMessage;
import com.putao.album.eventbus.BasePostEvent;
import com.putao.album.util.CommonUtils;
import com.putao.album.util.Loger;
import com.putao.album.util.PuTaoConstants;
import com.umeng.message.entity.UMessage;

import org.json.JSONObject;

import de.greenrobot.event.EventBus;

/**
 * Created by jidongdong on 15/5/14.
 */
public class InMessageUtil {

    /**
     * {
     * "header_photo": "",
     * "baby_photo": "",
     * "message_content": "爸爸取消了对测试2的关注，您将成为新的管理员",
     * "baby_id": "1",
     * "type": 2,
     * "photo_id": "",
     * "album_id": ""
     * }
     *
     * @param message
     */
    public static void dealMessage(UMessage message) {
        if (message == null) {
            return;
        }
        String content = message.custom;
        Loger.d("message content:" + content);
        JSONObject obj = checkJsonObject(content);
        if (obj != null) {
            String header_photo = obj.optString("header_photo");
            String baby_photo = obj.optString("baby_photo");
            String message_content = obj.optString("message_content");

            String photo_id = obj.optString("photo_id");
            String album_id = obj.optString("album_id");
            int type = obj.optInt("type");
            Long baby_id = obj.optLong("baby_id");

            addNewMessage(header_photo, baby_photo, message_content, baby_id, type, photo_id, album_id);
        }
    }

    /**
     * 添加一条记录到数据库
     *
     * @param headerPhoto
     * @param babyPhoto
     * @param content
     */
    static void addNewMessage(String headerPhoto, String babyPhoto, String content, Long baby_id, int type, String photo_id, String album_id) {
        InMessage message = new InMessage();
        message.setBaby_picture(babyPhoto);
        message.setCreatetime(System.currentTimeMillis());
        message.setIsread(0);
        message.setMessage_content(content);
        message.setHeader_photo(headerPhoto);
        message.setBaby_id(baby_id);
        message.setType(type);
        message.setPhoto_id(photo_id);
        message.setAlbum_id(album_id);
        new MessageHelper(GlobalApplication.getInstance()).addInMessage(message);
        CommonUtils.setNewMessageFlag(true);
        if (type == 1 || type == 2) {
            Bundle bundle = new Bundle();
            bundle.putSerializable("message", message);
            bundle.putInt("type", type);
            EventBus.getDefault().post(new BasePostEvent(PuTaoConstants.EVENTBUS_RECEIVE_MESSAGE, bundle));
        }
    }

    /**
     * 检测json字符串
     *
     * @param jsonstr
     * @return
     */
    static JSONObject checkJsonObject(String jsonstr) {
        try {
            JSONObject obj = new JSONObject(jsonstr);
            return obj;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
